import 'package:flutter/material.dart';
import 'package:flutter_project/common/route_page/route_name.dart';
import 'package:flutter_project/common/utils/screen_size_utils.dart';
import 'package:flutter_project/config/theme_color.dart';
import '../requst/dio_http_utils.dart';
import 'home_video_details_page.dart';
import 'home_video_list_model.dart';

class HomeVideoItemView extends StatefulWidget {
  final VideoListVideosModel itemModel;
  final Function? tapAction;
  final bool isCollect;

  const HomeVideoItemView({required this.itemModel, this.tapAction,this.isCollect=false, super.key});

  @override
  State<HomeVideoItemView> createState() => _HomeVideoItemViewState();
}

class _HomeVideoItemViewState extends State<HomeVideoItemView> {
  ValueNotifier<int> likeInt = ValueNotifier<int>(0);

  @override
  Widget build(BuildContext context) {
    likeInt.value = widget.itemModel.userLike ?? 0;
    double imgWidth = ScreenSizeUtils.deviceScreenWidth(context);
    imgWidth = (imgWidth - 10 * 3) / 2.0;
    return GestureDetector(
      onTap: () {
        RouteNames.push(HomeVideoDetailsPage(
          id: widget.itemModel.id??0,
        ));
      },
      child: Container(
        color: ThemeColor.mainBgColor,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Image.network(
              widget.itemModel.webp ?? '',
              height: 120,
              width: imgWidth,
              fit: BoxFit.cover,
            ),
            const SizedBox(
              height: 10,
            ),
            Text(
              widget.itemModel.prompt ?? '',
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
            ),
            ValueListenableBuilder(
                valueListenable: likeInt,
                builder: (BuildContext context, int value, Widget? child) {
                  return GestureDetector(
                    onTap: () {
                      clickLikeAndUnLikeAction(widget.itemModel);
                    },
                    child: Container(
                      color: ThemeColor.mainBgColor,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Icon(
                            Icons.favorite,
                            color: value == 1
                                ? Colors.red
                                : ThemeColor.textColorSecond,
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Text((widget.itemModel.likes ?? 0).toString()),
                          const SizedBox(
                            width: 10,
                          ),
                        ],
                      ),
                    ),
                  );
                }),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    widget.itemModel.userLike ?? 0;
    widget.itemModel.likes ?? 0;
  }

  void clickLikeAndUnLikeAction(VideoListVideosModel itemModel) async {
    String apiUrl = 'media/likeMedia';
    if (likeInt.value == 1) {
      apiUrl = 'media/unlikeMedia';
    }
    Map<String, dynamic>? dataMap = {
      'MediaId': itemModel.id,
      'MediaType': 1,
    };
    var res = await HttpDioUtils.request(apiUrl, params: dataMap);
    if (res != null && res['ErrCode'] == 0) {
        if (likeInt.value == 0) {
          likeInt.value = 1;
        } else {
          likeInt.value  = 0;
        }
        widget.itemModel.userLike=likeInt.value;
        if(likeInt.value==1){
          widget.itemModel.likes=(widget.itemModel.likes??0)+1;
        }else{
          widget.itemModel.likes=(widget.itemModel.likes??0)-1;
        }

        if(widget.tapAction!=null){
          widget.tapAction!();
        }
    }
  }
}
